package com.example.work4

import android.Manifest
import android.content.Intent
import android.content.pm.PackageManager
import android.graphics.Color
import android.os.Bundle
import android.view.View
import android.view.WindowManager
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.ViewModelProvider
import com.example.work4.databinding.ActivityLoginBinding
import com.example.work4.http.SSORetrofitImp
import com.example.work4.po.LoginInfo

class LoginActivity: AppCompatActivity()
{
    private var ssoRetrofitImp: SSORetrofitImp? = null
    var loginInfo: LoginInfo = LoginInfo()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val loginBinding: ActivityLoginBinding = DataBindingUtil.setContentView(this,
            R.layout.activity_login
        )
        loginBinding.lifecycleOwner = this
        loginInfo = ViewModelProvider(this, ViewModelProvider.NewInstanceFactory()).get(LoginInfo::class.java)
        loginBinding.userInfo = loginInfo
        ssoRetrofitImp=SSORetrofitImp(this)
        setStatusBar()
        check()
    }

    private fun setStatusBar() {
        val window = window
        val decorView = window.decorView
        val option = (View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                or View.SYSTEM_UI_FLAG_LAYOUT_STABLE)
        decorView.systemUiVisibility = option
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
        window.statusBarColor = Color.WHITE
    }

    fun check() {
        if (ContextCompat.checkSelfPermission(
                this,
                Manifest.permission.CAMERA
            ) != PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(
                this,
                Manifest.permission.RECORD_AUDIO
            ) != PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(
                this,
                Manifest.permission.WRITE_EXTERNAL_STORAGE
            ) != PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(
                this,
                Manifest.permission.READ_EXTERNAL_STORAGE
            ) != PackageManager.PERMISSION_GRANTED
        ) {
            ActivityCompat.requestPermissions(
                this, arrayOf(
                    Manifest.permission.CAMERA,
                    Manifest.permission.RECORD_AUDIO,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE,
                    Manifest.permission.READ_EXTERNAL_STORAGE
                ), 100
            )
        }
    }

    fun Login(view: View) {

        if(loginInfo.password.get()==null &&loginInfo.account.get()==null ){
            ssoRetrofitImp?.login(
                "2946263724@qq.com", "123123"
            )
            return
        }
        loginInfo.password.get()?.let {
            loginInfo.account.get()?.let { it1 ->
                ssoRetrofitImp?.login(
                    it1, it
                )
            }
        }
    }

    fun toRegister(view:View){
        val intent = Intent()
        intent.setClass(this,RegisterActivity::class.java)
        startActivity(intent)
    }
}